//
//  PopoverAnimator.swift
//  Weibo007
//
//  Created by 闫崇 on 15/12/17.
//  Copyright © 2015年 闫崇. All rights reserved.
//

import UIKit


/// UIViewControllerTransitioningDelegate 提供转场动画相关的对象
/// UIViewControllerAnimatedTransitioning 提供转场动画的实现
class PopoverAnimator: NSObject,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning {

    //展现视图的大小
    var presentFrame = CGRectZero
    //是否展现的标记
    var isPresent = false
    
    //返回负责转场的控制器
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        
        let pop = PopPresentationController(presentedViewController: presented, presentingViewController: presenting)
        //设置展现视图的 frame
        pop.presentFrame = presentFrame
        
        return pop
        
    }
    
    //返回提供 Modal 的对象
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        
        return self
        
    }
    
    //返回提供 Dismiss 动画的对象
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        
        return self
    }
    
    
    //动画时常
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        
        return isPresent ?  1.2 : 0.25 
    }
    
    
    //转场动画实现函数  一旦实现给该函数,默认的动画失效,所有的动画有程序员提供
    //transitionContext 转场上下文 : 提供转场所需的参数
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        //判断是展现还是 dismiss
        if isPresent {
            //获取视图
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
            
            //将 toView 添加到容器视图
            transitionContext.containerView()?.addSubview(toView)
            
            //动画实现
            //对展现的视图的高 变为0 宽不变
            toView.transform = CGAffineTransformMakeScale(1.0, 0)
            toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
            
            UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
                
                //让展现视图回复原貌
                toView.transform = CGAffineTransformIdentity
                
                }, completion: { (_) -> Void in
                    
                    //结束动画告诉系统动画已经结束
                    transitionContext.completeTransition(true)
                    
            })
            
            
            
        }else{
            
            //取消动画   fromView 删除
            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
    
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                //y 值设置一个非常小的数 就可以动画了
                fromView.transform = CGAffineTransformMakeScale(1.0, 0.000001)
                
                }, completion: { (_) -> Void in
                    //2.删除 fromView 展现的视图
                    fromView.removeFromSuperview()
                    //3.结束动画   告诉动画已经结束
                    transitionContext.completeTransition(true)
            })
            
            
            
     
        }
        
        
        
        
        
    }

    
    
    
}
